<?php defined('SYSPATH') OR die('No Direct Script Access');

Class Controller_Login extends Controller_PublicTemplate { 
	
	public function action_index() 
	{ 
		
		$this->template->title = 'Login';
		$view = new View('pages/login');
		
		// If someone tried to login
		if(isset($_POST['submit'])) 
		{
			$user = new Model_User();
			$users = $user->select_user($_POST['name'], $_POST['password']);
			if(count($users) == 1) 
			{ 
				$this->session = Session::instance();
				$this->session->set('id', (int) $users['user']['id']);
				$this->session->set('name', (string) $users['user']['name']);
				$this->session->set('password', (string) $users['user']['password']);
				$this->session->set('grocery_list_id', (int) $users['user']['listid']);
				$this->request->redirect('home');
				$message = 'U bent nu ingelogd.';
			}
			else 
			{
				$message = 'Onjuiste combinatie van naam en wachtwoord, probeer het opnieuw.';
			}
			
			
		}
		
		$view->bind('message', $message);
		$this->template->content = $view;
	}
}
